/**
 * 
 */
package com.cipe.cmrs.service.ref;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.dao.BaseRefDao;
import com.cipe.cmrs.model.ref.BaseRef;

/**
 * @author Incepio
 *
 */

@Transactional
public class BaseRefService{

	@Autowired
	protected BaseRefDao refDao;

	/**
	 * For calling the functiion and native query
	 * @param nativeQueryName
	 * @return
	 */
	
	@SuppressWarnings("rawtypes")
	public List getNamedNativeQueryResultList(String nativeQueryName){
		return refDao.getNamedNativeQueryResultList(nativeQueryName, null);
	}
	
	/**
	 * Add a new entity.
	 * 
	 * @param entity
	 * @return
	 */
	public BaseRef persist(BaseRef entity) {		
		return refDao.persist(entity);
	}	
	
	/**
	 * Merge the state of the given entity into the current persistence context. <br>
	 * @param entity
	 * @return the updated entity instance
	 */	
	public BaseRef update(BaseRef entity){
		return this.refDao.update(entity);
	}	
	
	/**
	 * Find the entity of T with a given id.
	 * @param id
	 * @return
	 */
	public BaseRef findById(Serializable id, Class<? extends BaseRef> persistanceClass) {
		return refDao.findById(persistanceClass, id);		
	}
	
	/**
	 * Returns all entities of type T.
	 * @return
	 */
	public List<BaseRef> findAll(Class<? extends BaseRef> persistanceClass) {
		return refDao.findAll(getEntityName(persistanceClass));	
	}
	
	public List<? extends BaseRef> findAllByType(Class<? extends BaseRef> persistanceClass) {
		return refDao.findAll(getEntityName(persistanceClass));	
	}
	
	/**
	 * Counts all the rows in the table for entity type T.
	 * @return count of all the entities of type T
	 */
	public Long countAll(Class<? extends BaseRef> persistanceClass) {
		return refDao.countAll(getEntityName(persistanceClass));		
	}
	
	
	
	/**
	 * The entity name for type T.
	 * @return
	 */
	protected String getEntityName(Class<? extends BaseRef> persistanceClass) {		
		if (persistanceClass == null) {
			throw new IllegalArgumentException("Persistent Class can not be null");
		}
		
		return persistanceClass.getName();
		
	}
	
}

